<?php
namespace app\admin\controller;

use app\common\model\UserRank as UserRankModel;
use app\common\controller\AdminBase;
use think\Config;
use think\Db;

/**
 * 用户等级管理
 * Class UserRank
 * @package app\admin\controller
 */
class UserRank extends AdminBase
{
    protected $user_rank_model;

    protected function _initialize()
    {
        parent::_initialize();
        $this->user_rank_model = new UserRankModel();
    }

    /**
     * 用户等级管理
     * @param string $keyword
     * @param int    $page
     * @return mixed
     */
    public function index($keyword = '', $page = 1)
    {
        $map = [];
        $userRank_list = $this->user_rank_model->order('id DESC')->paginate(15, false, ['page' => $page]);
        return $this->fetch('index', ['userRank_list' => $userRank_list]);
    }

    /**
     * 添加用户
     * @return mixed
     */
    public function add()
    {
        return $this->fetch();
    }

    /**
     * 保存用户
     */
    public function save()
    {
        if ($this->request->isPost()) {
            $data            = $this->request->post();
            $validate_result = $this->validate($data, 'User');

            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                $data['password'] = md5($data['password'] . Config::get('salt'));
                if ($this->user_rank_model->allowField(true)->save($data)) {
                    $this->success('保存成功');
                } else {
                    $this->error('保存失败');
                }
            }
        }
    }

    /**
     * 编辑用户等级
     * @param $id
     * @return mixed
     */
    public function edit($id)
    {
        $rank_info = $this->user_rank_model->find($id);
        return $this->fetch('edit', ['rank_info' => $rank_info]);
    }

    /**
     * 更新用户等级
     * @param $id
     */
    public function update($id)
    {
        if ($this->request->isPost()) {
            $data            = $this->request->post();
            $validate_result = $this->validate($data, 'UserRank');

            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                $rank_info               = $this->user_rank_model->find($id);
                $rank_info->id           = $id;
                $rank_info->rank_name    = $data['rank_name'];
                $rank_info->reg_money    = $data['reg_money'];
                $rank_info->reg_integral = $data['reg_integral'];
                $rank_info->day_consume  = $data['day_consume'];
                $rank_info->sum_consume  = $data['sum_consume'];
                if ($rank_info->save() !== false) {
                    $this->success('更新成功');
                } else {
                    $this->error('更新失败');
                }
            }
        }
    }

    /**
     * 删除用户
     * @param $id
     */
    public function delete($id)
    {
        if ($this->user_rank_model->destroy($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
}